<?php

global $appCfg;
$appCfg=array();
$appCfg['Url']="//".$_SERVER['SERVER_NAME'];

function customError($errno, $errstr, $errfile, $errline)
 { 
 	echo "<pre>";
 	print_r("Error Code:".$errno."\n");
 	print_r("File:".$errfile."\n");
 	print_r("Line:".$errline."\n");
 	print_r("Error Msg:".$errstr."\n");
 	echo "</pre>";
 	die();
  }
  
 function customException($exception)
{
	echo "<pre>";
 	print_r($exception);
 	echo "</pre>";
 	die();
}

set_exception_handler('customException');
set_error_handler("customError");
 
function GetDataBase()
{
	if (Zend_Registry::isRegistered('DB')) 
	{
		return Zend_Registry::get('DB');
	}
	//$params = array ('dbname' => 'db/passport.sdb');
	$params = array ('dbname' => 'db/passport.s3db');
	$db = Zend_Db::factory('PDO_SQLITE', $params);
	Zend_Registry::set('DB', $db);
	return $db;
}

function ShowVar($var)
{
  	echo "<pre>";
  	print_r($var);
  	echo "<pre>";
  	die();
}

function GetMemcache()
{
	if (Zend_Registry::isRegistered('MC')) 
	{
		return Zend_Registry::get('MC');
	}		
	$memcache = new Memcache;
	$memcache->connect('127.0.0.1', 11211);	
	Zend_Registry::set('MC', $memcache);
	return $memcache;
}

function RedirectToUrl($newUrl)
{
	echo "<html><head><title>Redirect</title>";
	echo "<script type='text/javascript'>\n";
	echo "if(parent!=null)\n";
	echo "top.window.location=\"".$newUrl."\";\n";
	echo "else\n";
	echo "window.location=\"".$newUrl."\";\n";
	echo "</script>\n";
	echo "</head><body/></html>";  
}

function RegAccount($email,$pass,$userName,$gender)
{
	$db=GetDataBase();
	$strSqlQuery="SELECT Id FROM account WHERE EMail=:em;";
	$q = $db->fetchRow($strSqlQuery,array('em'=>$email));
	if($q==null)
	{
		$ctx = hash_init('md5');
		hash_update($ctx,$pass);
		$pass=hash_final($ctx);
		$strSql="INSERT INTO account(EMail,UserPassport,Gender,UserName) VALUES(:em,:p,:g,:un);";
		$db->query($strSql,
				array('em'=>$email,
					'p'=>$pass,
					'g'=>$gender,
					'un'=>$userName));
		return 0;
	}
	return -1;
}
?>